#==============================================================================#
#                                                                              #
#                                CHAPTER FIVE                                  #
#                                                                              #
#               When civil war is the norm: Experimenting with                 #
#                            Colombian institutions                            #
#                                                                              #
#                                    2022                                      #
#==============================================================================#
# Working_directory 
#
setwd("C:/")

library(dplyr)
install.packages("glm2")
library(glm2)
install.packages("pscl")
library(pscl)
install.packages("janitor")
library(janitor)
library(MASS)
install.packages("gam")
library(gam)
install.packages("mgcv")
library(mgcv)
#
#To export the results
library(openxlsx)
library(rio)
install_formats("csvy", "feather","fst", "hexView", "jsonlite", "readODS", 
                "rmatio", "xml2", "yaml")
#
#Summary data
#
library(FactoMineR)
library(factoextra)
install.packages("Factoshiny")
library(Factoshiny)
result<-Factoshiny(Congress)
Edu_Spec <- CA(Congress$Education, graph = FALSE)
fviz_ca_biplot(Edu_Spec, repel = TRUE)
# Logistic models independent behaviors
#
Congress<-Colombian_Congress_Peace_Data
senators<-filter(Colombian_Congress_Peace_Data, Position == "Senator")
rep<-filter(Colombian_Congress_Peace_Data, Position == "Representative")
# Congress in full 
Model_A1 <- glm2(formula = Law_1909~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                 factor(Media), data=Colombian_Congress_Peace_Data, 
                 family=binomial(link = "logit"))
summary(Model_A1)
Model_A2 <- glm2(formula = Law_895~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                 factor(Media), data=Colombian_Congress_Peace_Data, 
                 family=binomial(link = "logit"))
summary(Model_A2)
Model_A3 <- glm2(formula = Law_885~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                   factor(Media), data=Colombian_Congress_Peace_Data, 
                 family=binomial(link = "logit"))
summary(Model_A3)
#Likelihood ratio test congress in full
ANOV_Model_A1<-anova(Model_A1, test ="Chisq")
ANOV_Model_A2<-anova(Model_A2, test = "Chisq")
ANOV_Model_A3<-anova(Model_A3, test = "Chisq")
export(ANOV_Model_A1, "ANOV_Model_A1.xlsx")
export(ANOV_Model_A2, "ANOV_Model_A2.xlsx")
export(ANOV_Model_A3, "ANOV_Model_A3.xlsx")
#Pseudo R square [McFadden] congress in full (pscl library)
pR2(Model_A1)  
pR2(Model_A2)
pR2(Model_A3) 
# Senate members
Model_S1 <- glm2(formula = Law_1909~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                  factor(Media), data=senators, family=binomial(link = "logit"))
summary(Model_S1)
Model_S2 <- glm2(formula = Law_895~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                  factor (Media), data=senators, family=binomial(link = "logit"))
summary(Model_S2)
Model_S3 <- glm2(formula = Law_885~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                  factor (Media), data=senators, family=binomial(link = "logit"))
summary(Model_S3)
#Likelihood ratio test senate
ANOV_Model_S1<-anova(Model_S1, test ="Chisq")
ANOV_Model_S2<-anova(Model_S2, test = "Chisq")
ANOV_Model_S3<-anova(Model_S3, test = "Chisq")
export(ANOV_Model_S1, "ANOV_Model_S1.xlsx")
export(ANOV_Model_S2, "ANOV_Model_S2.xlsx")
export(ANOV_Model_S3, "ANOV_Model_S3.xlsx")
#Pseudo R square [McFadden] congress in full (pscl library)
pR2(Model_S1)  
pR2(Model_S2)
pR2(Model_S3) 
# Chamber members
Model_Ch1 <- glm2(formula = Law_1909~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                 +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                   factor(Media), data=rep, family=binomial(link = "logit"))
summary(Model_Ch1)
Model_Ch2 <- glm2(formula = Law_895~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                  +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                    factor(Media), data=rep, family=binomial(link = "logit"))
summary(Model_Ch2)
Model_Ch3 <- glm2(formula = Law_885~factor(Kinship)+factor(Funds)+factor(Perpetuation)
                  +factor(Spectrum)+factor(Criteria)+factor(Religion)+factor(Military)+ 
                    factor(Media), data=rep, family=binomial(link = "logit"))
summary(Model_Ch3)
#Likelihood ratio test senate
ANOV_Model_Ch1<-anova(Model_Ch1, test ="Chisq")
ANOV_Model_Ch2<-anova(Model_Ch2, test = "Chisq")
ANOV_Model_Ch3<-anova(Model_Ch3, test = "Chisq")
export(ANOV_Model_Ch1, "ANOV_Model_Ch1.xlsx")
export(ANOV_Model_Ch2, "ANOV_Model_Ch2.xlsx")
export(ANOV_Model_Ch3, "ANOV_Model_Ch3.xlsx")
#Pseudo R square [McFadden] congress in full (pscl library)
pR2(Model_Ch1)  
pR2(Model_Ch2)
pR2(Model_Ch3) 
#
# Groups
#
Self_enforcers<-filter(Congress, Reply_days<=30)
tabyl(Self_enforcers, Position)
Dodgers<-filter(Congress, Reply_days>=31 & Reply_days<=110)
tabyl(Dodgers, Position)
Scofflaw<-filter(Congress, Reply_days>=111)
tabyl(Scofflaw, Position)
#
#==================Generalized Additive Models (GAM) self-enforcers
#
dat$group <- factor(dat$group)
Self_enforcers$Kinship<-factor(Self_enforcers$Kinship)
Self_enforcers$Funds<-factor(Self_enforcers$Funds)
Self_enforcers$Perpetuation<-factor(Self_enforcers$Perpetuation)
Self_enforcers$Spectrum<-factor(Self_enforcers$Spectrum)
Self_enforcers$Criteria<-factor(Self_enforcers$Criteria)
Self_enforcers$Religion<-factor(Self_enforcers$Religion)
Self_enforcers$Military<-factor(Self_enforcers$Military)
Self_enforcers$Media<-factor(Self_enforcers$Media)
Model_SR1Gam<-gam(Law_1909~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Self_enforcers) 
summary(Model_SR1Gam)
Model_SR2Gam<-gam(Law_885~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Self_enforcers)
summary(Model_SR2Gam)
Model_SR3Gam<-gam(Law_895~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Self_enforcers)
summary(Model_SR3Gam)
#
#GAM Dodgers
Dodgers$Position<-factor(Dodgers$Position)
Dodgers$Kinship<-factor(Dodgers$Kinship)
Dodgers$Funds<-factor(Dodgers$Funds)
Dodgers$Perpetuation<-factor(Dodgers$Perpetuation)
Dodgers$Spectrum<-factor(Dodgers$Spectrum)
Dodgers$Criteria<-factor(Dodgers$Criteria)
Dodgers$Religion<-factor(Dodgers$Religion)
Dodgers$Military<-factor(Dodgers$Military)
Dodgers$Media<-factor(Dodgers$Media)
Model_Dgr1Gam<-gam(Law_1909~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Dodgers) 
summary(Model_Dgr1Gam)
Model_Dgr2Gam<-gam(Law_885~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Dodgers)
summary(Model_Dgr2Gam)
Model_Dgr3Gam<-gam(Law_895~Kinship+Funds+Perpetuation+Spectrum+Criteria
                  +Religion+Military+Media, data=Dodgers)
summary(Model_Dgr3Gam)
#
#GAM Scofflaw
#
Scofflaw$Position<-factor(Scofflaw$Position)
Scofflaw$Kinship<-factor(Scofflaw$Kinship)
Scofflaw$Funds<-factor(Scofflaw$Funds)
Scofflaw$Perpetuation<-factor(Scofflaw$Perpetuation)
Scofflaw$Spectrum<-factor(Scofflaw$Spectrum)
Scofflaw$Criteria<-factor(Scofflaw$Criteria)
Scofflaw$Religion<-factor(Scofflaw$Religion)
Scofflaw$Military<-factor(Scofflaw$Military)
Scofflaw$Media<-factor(Scofflaw$Media)
Model_Scf1Gam<-gam(Law_1909~Kinship+Funds+Perpetuation+Spectrum+Criteria
                   +Religion+Military+Media, data=Scofflaw) 
summary(Model_Scf1Gam)
Model_Scf2Gam<-gam(Law_885~Kinship+Funds+Perpetuation+Spectrum+Criteria
                   +Religion+Military+Media, data=Scofflaw)
summary(Model_Scf2Gam)
Model_Scf3Gam<-gam(Law_895~Kinship+Funds+Perpetuation+Spectrum+Criteria
                   +Religion+Military+Media, data=Scofflaw)
summary(Model_Scf3Gam)
#
#GAM for attributes per experimental group
#
#Self-enforcers
Self_enforcers$PositionF<-factor(Self_enforcers$Position)
Self_enforcers$GenderF<-factor(Self_enforcers$Gender)
Self_enforcers$PartyF<-factor(Self_enforcers$Party)
Self_enforcers$EducationF<-factor(Self_enforcers$Education)
Self_enforcers$InvestigatedF<-factor(Self_enforcers$Investigated)
GamMSF1909Att<-gam(Law_1909~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                   data = Self_enforcers)
summary(GamMSF1909Att)
capture.output(summary(GamMSF1909Att), file="GamMSF1909Att.txt")
GamMSFAge1909<-gam(Law_1909~age_group, data = Self_enforcers)
summary(GamMSFAge1909)
capture.output(summary(LinearMSFAge), file="amMSFAge1909.txt")
GamMSF885Att<-gam(Law_885~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                 data = Self_enforcers)
summary(GamMSF885Att)
capture.output(summary(GamMSF885Att), file="GamMSF885Att.txt")
GamMSF885Age<-gam(Law_885~age_group, data = Self_enforcers)
summary(GamMSF885Age)
capture.output(summary(GamMSF885Age), file="GamMSF885Age.txt")
GamMSF895Att<-gam(formula = Law_895~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                 data = Self_enforcers)
summary(GamMSF895Att)
capture.output(summary(GamMSF895Att), file="GamMSF895Att.txt")
GamMSF895Age<-gam(Law_895~age_group, data = Self_enforcers)
summary(GamMSF895Age)
capture.output(summary(GamMSF895Age), file="GamMSF895Age.txt")
#
#Dodgers
Dodgers$PositionF<-factor(Dodgers$Position)
Dodgers$GenderF<-factor(Dodgers$Gender)
Dodgers$PartyF<-factor(Dodgers$Party)
Dodgers$EducationF<-factor(Dodgers$Education)
Dodgers$InvestigatedF<-factor(Dodgers$Investigated)
GamDgr1909Att<-gam(Law_1909~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
              data = Dodgers)
summary(GamDgr1909Att)
capture.output(summary(GamDgr1909Att), file="GamDgr1909Att.txt")
GamDgr1909Age<-gam(Law_1909~age_group, data = Dodgers)
summary(GamDgr1909Age)
capture.output(summary(LinearDgr1909Age), file="LinearDgr1909Age.txt")
GamDgr885Att<-gam(Law_885~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                   data = Dodgers)
summary(GamDgr885Att)
capture.output(summary(GamDgr885Att), file="GamDgr885Att.txt")
GamDgr885Age<-gam(Law_885~age_group, data = Dodgers)
summary(GamDgr885Age)
capture.output(summary(GamDgr885Age), file="GamDgr885Age.txt")
GamDgr895Att<-gam(Law_895~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                  data = Dodgers)
summary(GamDgr895Att)
capture.output(summary(GamDgr895Att), file="GamDgr895Att.txt")
GamDgr895Age<-gam(Law_895~age_group, data = Dodgers)
summary(GamDgr895Age)
capture.output(summary(GamDgr885Age), file="GamDgr885Age.txt")
#
#Scofflaw
Scofflaw$PositionF<-factor(Scofflaw$Position)
Scofflaw$GenderF<-factor(Scofflaw$Gender)
Scofflaw$PartyF<-factor(Scofflaw$Party)
Scofflaw$EducationF<-factor(Scofflaw$Education)
Scofflaw$InvestigatedF<-factor(Scofflaw$Investigated)
GamScf1909Att<-gam(Law_1909~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                   data = Scofflaw)
summary(GamScf1909Att)
capture.output(summary(GamScf1909Att), file="GamScf1909Att.txt")
GamScf1909Age<-gam(Law_1909~age_group, data = Scofflaw)
summary(GamScf1909Age)
capture.output(summary(gamScf1909Age), file="LinearScf1909Age.txt")
GamScf885<-gam(formula = Law_885~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
                 data = Scofflaw)
summary(GamScf885)
capture.output(summary(GamScf885), file="GamScf885.txt")
GamScf885Age<-gam(Law_885~age_group, data = Scofflaw)
summary(GamScf885Age)
capture.output(summary(GamScf885Age), file="GamScf885Age.txt")
GamScf895<-gam(formula = Law_895~PositionF+GenderF+PartyF+EducationF+InvestigatedF, 
               data = Scofflaw)
summary(GamScf895)
capture.output(summary(GamScf895), file="GamScf885.txt")
GamScf895Age<-gam(Law_895~age_group, data = Scofflaw)
summary(GamScf895Age)
capture.output(summary(GamScf895Age), file="GamScf895Age.txt")
# -------------------------------------------------------------------- #
